function createTag(name, attributes, innerHTML) {
    return "<" + name + (attributes == "" ? "" : " ") + attributes + ">" + innerHTML + "</" + name + ">";
}

function createEditModeSection(section) {
    var boxes = section.hBoxes;
    var iLength = boxes.length;
    var html = "";
    var lableValueSpacing = 5;
    for(var i=0; i<iLength; i++) {
        var box = boxes[i];
        var str = "";
        if(box.both != null) {
            str += createTag("td", "style=\"width:1%\" nowrap", box.both.name);
            str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
            str += createTag("td", "colspan=4", createField(box.both));
        }
        else {
            if(box.left != null) {
                str += createTag("td", "style=\"width:1%\" nowrap", box.left.name);
                str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
                str += createTag("td", "style=\"width:50%\"", createField(box.left));
            }
            else {
                str += createTag("td", "colspan=3", "&nbsp;");
            }
            if(box.right != null) {
                str += createTag("td", "style=\"width:1%\" nowrap", box.right.name);
                str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
                str += createTag("td", "", createField(box.right));
            }
            else {
                str += createTag("td", "colspan=3", "&nbsp;");
            }
        }
        html += createTag("tr", "", str);
    }
    html = createTag("table", "border='1' width='100%'", html);
    var legend = createTag("legend", "", section.name);
    html = createTag("fieldset", "", legend + html);
    return html;
}

function createField(field) {
    var tabIndex = "";
    var html = "";
    if(field.type == "STRING_SMALL") {
        html = "<input type=\"text\" name=\"" + field.type + field.pk + "\" " + tabIndex + "class=\"ChampsForm\" maxlength=\"15\" size=\"15\" value=\"" + field.value + "\" isMandatory=\"" + field.isMandatory + "\" translatedName=\"" + field.name + "\"" + ">";
    }
    else if(field.type == "STRING_LARGE") {
        html = "<input type=\"text\" name=\"" + field.type + field.pk + "\" " + tabIndex + "class=\"ChampsForm\" maxlength=\"250\" size=\"35\" value=\"" + field.value + "\" isMandatory=\"" + field.isMandatory + "\" translatedName=\"" + field.name + "\"" + ">";
    }
    return html;
}

function createSection(section, isEditMode) {
    if(isEditMode) {
        return createEditModeSection(section);
    }
    var boxes = section.hBoxes;
    var iLength = boxes.length;
    var html = "";
    var lableValueSpacing = 5;
    for(var i=0; i<iLength; i++) {
        var box = boxes[i];
        var str = "";
        if(box.both != null) {
            str += createTag("td", "width=\"1%\" nowrap", box.both.name);
            str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
            str += createTag("td", "colspan=4", box.both.value);
        }
        else {
            if(box.left != null) {
                str += createTag("td", "width=\"1%\" nowrap", box.left.name);
                str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
                str += createTag("td", "", box.left.value);
            }
            else {
                str += createTag("td", "colspan=3", "&nbsp;");
            }
            if(box.right != null) {
                str += createTag("td", "width=\"1%\" nowrap", box.right.name);
                str += createTag("td", "style=\"width:" + lableValueSpacing + "px\" nowrap", "");
                str += createTag("td", "", box.right.value);
            }
            else {
                str += createTag("td", "colspan=3", "&nbsp;");
            }
        }
        html += createTag("tr", "", str);
    }
    html = createTag("table", "border='1' width='100%'", html);
    var legend = createTag("legend", "", section.name);
    html = createTag("fieldset", "", legend + html);
    return html;
}
